package com.demo.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.domain.WorkIssue;
import com.demo.model.R;
import com.demo.service.WorkIssueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/workIssues")
@RestController
public class WorkIssueController {

    @Autowired
    private WorkIssueService workIssueService;

    @GetMapping
    public R<Page<WorkIssue>> findByPage(Page<WorkIssue> page,Integer status,String startTime,String endTime){
        page.addOrder(OrderItem.desc("last_update_time"));
        return R.ok(workIssueService.findByPage(page,status,startTime,endTime));
    }


    @PatchMapping
    public R work_issue_update(Long id,String answer){
        WorkIssue workIssue = new WorkIssue();
        workIssue.setId(id);
        workIssue.setAnswer(answer);
        //设置回复人ID
        String userIdStr = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        workIssue.setAnswerUserId(Long.valueOf(userIdStr));

        boolean update = workIssueService.updateById(workIssue);
        if(update){
            return  R.ok("修改成功");
        }
        return R.fail("修改失败");

    }
}
